package com.vng.zing.lib;

/**
 *
 * @author redphx
 */
public class Cover {
    private static String COVER_URL = Config.getParam("setting", "cover_url");
    private static String COVER_DEFAULT = Config.getParam("setting", "cover_default");
    
    public static String getUrl(String username, long version) {
        if (version == 0) {
            return COVER_DEFAULT;
        }
        
        String path = getPath(username);
        String filename = getFilename(username, version);
        
        return COVER_URL + "/" + path + "/" + filename;
    }
    
    public static String getPath(String username) {
        String hashUsername = Hash.md5(username);
        StringBuilder sb = new StringBuilder();
        sb.append(hashUsername.substring(0, 2)).append("/");
        sb.append(hashUsername.substring(2, 4)).append("/");
        sb.append(hashUsername.substring(4, 6));
        
        return sb.toString();
    }
    
    public static String getFilename(String username, long version) {
        String hashVersion = Hash.md5(username + "-" + version + "-" + "zxcasdqwe");
        StringBuilder sb = new StringBuilder();
        sb.append(username).append("_").append(hashVersion.substring(0, 6)).append("_").append(version).append(".jpg");
        return sb.toString();        
    }
    
}
